package name.swingeet.swingElf;
/*
 * Copyright 2005-2010 Ignis Software Tools Ltd. All rights reserved.
 */


import java.awt.Component;

import javax.swing.JComboBox;

import name.swingeet.swingtest.JemmySupport;
import name.swingeet.swingtest.TipNameButtonFinder;

import org.netbeans.jemmy.ClassReference;
import org.netbeans.jemmy.operators.ContainerOperator;
import org.netbeans.jemmy.operators.JButtonOperator;
import org.netbeans.jemmy.operators.JCheckBoxOperator;
import org.netbeans.jemmy.operators.JComboBoxOperator;
import org.netbeans.jemmy.operators.JDialogOperator;
import org.netbeans.jemmy.operators.JFrameOperator;
import org.netbeans.jemmy.operators.JRadioButtonOperator;



/**
 * This class to written to create a generic support for working with Jemmy
 * support this class is used to operate a GUI written in swing automatically
 */
public class SwingTestImpl implements SwingTestInterface {
	
	static JemmySupport js=new JemmySupport();
	
	
	public void setTextInFieldOperator(String mainWindow,
			 int indexNum,String value)throws Exception{
		JFrameOperator jfo=new JFrameOperator(mainWindow);
		js.setTextInFieldOperator(jfo, indexNum, value);
	}
	@Override
	public void ReferenceApp(String clazz) throws Exception{
		try {
			
			new ClassReference(clazz);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Override
	public String SystemOutPrintTest(String text) throws Exception{
		System.out.println(text);
		return "Hello World "+text; 
	}
	
	/**
	 * This method is designed to push a button and for a dialog window to be opened.
	 * The method also activates a retry mechanism until the dialog is successfully opened.
	 * 
	 * @param windowName
	 *            The window the button exist in (Label of the window)
	 * @param buttonName
	 *            The button name to push
	 * @param mainWindow
	 *            The dialog window that will be opened.
	 * @throws Exception
	 */
	
	@Override
	public void pushButtonAndWaitForDialog(String mainWindow,
			String buttonName, String windowName) throws Exception {
		js.pushButtonAndWaitForDialog(mainWindow, buttonName, windowName);
	}

	
	@Override
	public void pushButtonAndWaitAndApproveDialog(String mainWindow,
			String buttonName, String windowName, String dialogButton)
			throws Exception {
		// Pressing the button
		js.pushButtonAndWaitAndApproveDialog(mainWindow, buttonName, windowName, dialogButton);

	}
	
	/**
	 * locate a component by name, inside a given container
	 * 
	 * @param operator
	 *            the container to look inside
	 * @param text
	 *            the text to find
	 * @return
	 */
	
	@Override
	public boolean isComponentDisplayed(String windowName, String text) throws Exception {
		JFrameOperator jfo=new JFrameOperator(windowName);
		Component c=js.locateComponent(jfo, text);
		if(c!=null)
			return true;
		else
			return false;
	}
	
	/**
	 * close a dialog window if opened
	 * 
	 * @param dialogName
	 *            the dialog window title
	 * @param confirmButton
	 *            the confirm button text
	 * @return true if a dialog was opened, false if none opened
	 * @throws Exception
	 */
	
	@Override
	public boolean ConfirmDialogIfExists(String dialogName, String confirmButton)
			throws Exception {
		return js.ConfirmDialogIfExists(dialogName, confirmButton);
	}
	
	/**
	 * Return state of a button identified by a String\Index
	 * 
	 * @param operator
	 *            the operator the button is inside
	 * @param identifier
	 *            the name\Index of the button
	 * @return isEnabled return false if disabled
	 * @throws Exception
	 */
	
	@Override
	public Boolean isButtonEnabled(String windowName, Object identifier)
			throws Exception {
		JFrameOperator jfo=new JFrameOperator(windowName);
		return js.isButtonEnabled(jfo, identifier);
	}
	
	/**
	 * select a given value in a combobox operator
	 * 
	 * @param operator
	 *            the operator to search the combobox in
	 * @param txt
	 *            the combobox title
	 * @param value
	 *            the value to set
	 */
	
	@Override
	public void selectInComboBox(String windowName, String txt,
			Object value) throws Exception{
		this.selectInComboBox(windowName, txt, value, 0);
	}

	/**
	 * select a given value in a combobox operator
	 * 
	 * @param operator
	 *            the operator to search the combobox in
	 * @param txt
	 *            the combobox title
	 * @param value
	 *            the value to set
	 * @param indexNum
	 *            how many operators to skip
	 */
	
	@Override
	public void selectInComboBox(String windowName, String txt,
			Object value, int indexNum) throws Exception {
		JFrameOperator jfo=new JFrameOperator(windowName);
		js.selectInComboBox(jfo, txt, value,indexNum);
	}
	
	/**
	 * checks if the dialog exists. if found, check if it is still visible in 1
	 * second intervals
	 * 
	 * @param dialogTitle
	 *            the dialog title
	 * @throws Exception
	 */
	
	@Override
	public void WaitForDialogToClose(String dialogTitle) throws Exception {
	js.WaitForDialogToClose(dialogTitle);
	}

	/**
	 * locate a radio button by a given identifier (name\index)
	 * 
	 * @param operator
	 *            the operator to search in
	 * @param identifier
	 *            the name\index to locate radioButton by
	 */
	
	@Override
	public void pushRadioButton(String windowName, Object identifier) throws Exception{
		JFrameOperator jfo=new JFrameOperator(windowName);
		js.pushRadioButton(jfo, identifier);
	}
	
	/**
	 * set a text in a JTextField operator
	 * 
	 * @param operator
	 *            the container to search in
	 * @param labelText
	 *            the label before the JTextField
	 * @param value
	 *            the value to insert
	 * @param indexNum
	 *            how many JTextFields to skip
	 */
	
	@Override
	public void setTextInFieldOperator(String windowName,
			String labelText, Object value, int indexNum) throws Exception {
		JFrameOperator jfo=new JFrameOperator(windowName);
	js.setTextInFieldOperator(jfo, labelText, value,indexNum);

	}
	
	
	/**
	 * set a text in the first JTextField operator found after the given label
	 * 
	 * @param operator
	 *            the container to search in
	 * @param labelText
	 *            the label before the JTextField
	 * @param value
	 *            the value to insert
	 */
	
	@Override
	public void setTextInFieldOperator(String windowName,
			String labelText, Object value) throws Exception {
		JFrameOperator jfo=new JFrameOperator(windowName);
		setTextInFieldOperator(windowName, labelText, value,0);
	}
	
	/**
	 * set a text in a JTextArea operator
	 * 
	 * @param operator
	 *            the container to search in
	 * @param labelText
	 *            the label before the JTextField
	 * @param value
	 *            the value to insert
	 * @param indexNum
	 *            how many JTextArea to skip
	 */
	
	@Override
	public void setTextInAreaOperator(String windowName,
			String labelText, Object value, int indexNum) throws Exception {
		JFrameOperator jfo=new JFrameOperator(windowName);
	js.setTextInFieldOperator(jfo, labelText, value,indexNum);

	}
	
	/**
	 * set a text in the first JTextArea operator found after the given label
	 * 
	 * @param operator
	 *            the container to search in
	 * @param labelText
	 *            the label before the JTextArea
	 * @param value
	 *            the value to insert
	 */
	
	@Override
	public void setTextInAreaOperator(String windowName,
			String labelText, Object value) throws Exception {
		JFrameOperator jfo=new JFrameOperator(windowName);
		setTextInAreaOperator(windowName, labelText, value,0);
	}
	@Override
	public void pushButton(String windowName, String buttonText)
			throws Exception {
		JFrameOperator jfo=new JFrameOperator(windowName);
		js.pushButton(jfo, buttonText);
		
	}
	
	
	/**
	 * This method sets checkbox condition to on or off.
	 * The method also activates a retry mechanism until successful
	 * 
	 * @param condition
	 *            true to set it on , false to set it to off
	 * @param windowName
	 *            The window name the checkbox exist in
	 * @param checkBoxName
	 *            The number of the checkbox in the window.
	 * @throws Exception
	 */
	@Override
	public void setCheckBox( String windowName,
			int checkBoxNumber,boolean condition) throws Exception {

	js.setCheckBox(condition, windowName, checkBoxNumber);

	}
	/**
	 * check/uncheck a checkbox
	 * 
	 * @param operator
	 *            the oprrator the checkbox is inside
	 * @param identifier
	 *            checkbox name\index
	 * @param enable
	 *            True will check, False will uncheck
	 */
	@Override
	public void setCheckBox(String windowName, String checkBoxName,
			boolean enable) throws Exception {
		JFrameOperator operator = new JFrameOperator(windowName);
		js.setCheckBox(operator, checkBoxName, enable);
	
	}
	/**
	 * Get checkbox status Enable/Disabled
	 */
	@Override
	public boolean isCheckBoxEnabled(String windowName, Object object)
			throws Exception {
		// TODO Auto-generated method stub
		JFrameOperator operator = new JFrameOperator(windowName);
		JCheckBoxOperator cbo=js.getCheckBoxOperator(operator, object);
		return cbo.isEnabled();
	}
	@Override
	public boolean pushButton(String windowName, Object obj, int notTry) throws Exception{
		// TODO Auto-generated method stub
		JFrameOperator operator = new JFrameOperator(windowName);
		return js.pushButton(operator, obj);

	}
	@Override
	public boolean isCheckBoxDisplayed(String windowName, String obj)
			throws Exception {
		JFrameOperator operator = new JFrameOperator(windowName);
		JCheckBoxOperator c=js.getCheckBoxOperator(operator, obj);
		if(c!=null)
			return true;
		else
			return false;

	}

}
